import json

import pdfplumber

import common

tag = "Icd9Cm3"
fields = ["Code", "Name"]
server = common.ServerRemoteRelease

if __name__ == "__main__":
    with pdfplumber.open("/Users/zhujianfeng/ICD-9-CM3医保2.0版.pdf") as pdf:
        for page in pdf.pages:
            # text = page.extract_text()#提取文本
            table = page.extract_table()
            # print(table)
            skipped_rows_table = table[1:]
            records = []
            for row in skipped_rows_table:
                # print(row[8])
                # print(row[9].replace('\n', ''))
                records.append({
                    fields[0]: row[8].strip(),
                    fields[1]: row[9].strip().replace('\n', '')
                })
            msg = json.dumps({
                "fields": fields,
                "records": records
            }, ensure_ascii=False)
            # print(msg)
            common.batch_add(tag, msg, server)
